package com.vimi8.ebb.service;

import com.vimi8.ebb.model.ApplyRecord;
import com.vimi8.ebb.model.TradingDetail;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;

/**
 * Created by think on 2016/9/8.
 * 申请记录service
 */
@Mapper
public interface ApplyRecordService {

    /**
     * 添加申请记录
     * @return
     */
@Insert("insert into apply_record(id,user_id,apply_person,ebb_id,remark,create_time)" +
            " values(#{id},#{userId},#{applyPerson},#{ebbId},#{remark},CURRENT_TIMESTAMP)")
    public int insertApplyRecord(ApplyRecord applyRecord);

    @Select("select count(id) from apply_record WHERE user_id=#{userId} AND ebb_id=#{ebbId}")
    public int getApplyRecordCount(@Param("ebbId") String ebbId,@Param("userId") String userId);

    /**
     * 查询申请记录
     * */
    @Select("<script>select count(user_id) from apply_record ar left join electricity_business e " +
            "on ar.ebb_id=e.id where 1=1 " +
            "<if test=\"userId!=null and userId!='' \"> and ar.`user_id` =#{userId} </if> " +
            "<if test=\"storeName!=null and storeName!='' \"> and e.`store_name` like concat('%',#{storeName},'%') </if> " +
            "<if test=\"approvalStatus!=null and approvalStatus!='' \"> and ar.`approval_status` =#{approvalStatus} </if> " +
            "</script>")
    public int queryApplyRecordCount(@Param("storeName") String storeName,@Param("approvalStatus") String approvalStatus,@Param("userId") String userId);
 /**
  * 查询申请记录
  * */
    @Select("<script>select ar.id as id,user_id as userId,apply_person as applyPerson,ebb_id as ebbId,e.store_name as ebbName," +
            "approval_status as approvalStatus,ar.remark,ar.create_time as createTime,ar.update_time as updateTime from apply_record ar left join electricity_business e " +
            "on ar.ebb_id=e.id where 1=1 " +
            "<if test=\"userId!=null and userId!='' \"> and ar.`user_id` =#{userId} </if> " +
            "<if test=\"storeName!=null and storeName!='' \"> and e.`store_name` like concat('%',#{storeName},'%') </if> " +
            "<if test=\"approvalStatus!=null and approvalStatus!='' \"> and ar.`approval_status` =#{approvalStatus} </if> " +
            "limit #{start},#{size}</script>")
    public List<ApplyRecord> queryTradeDetailById(@Param("storeName") String storeName,@Param("approvalStatus") String approvalStatus,@Param("userId") String userId,@Param("start") int start,@Param("size") int size);


    /**
     * 修改申请状态  状态字段和填写备注
     * @param applyRecord
     * @return
     */
    @Update("update apply_record set approval_status=#{approvalStatus},remark=#{remark},update_time=CURRENT_TIMESTAMP where id = #{id}")
    public int updateApplyStatus(ApplyRecord applyRecord);

    /**
     * 删除记录
     * @param id
     * @return
     */
    @Delete("delete from apply_record where id = #{id}")
    public int deleteeApplyStatus(String id);

}



